package com.sdtv.mediunfusion.widget;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.AutoCompleteTextView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.sdtv.mediunfusion.R;
import com.sdtv.mediunfusion.bean.StatusBean;
import com.sdtv.mediunfusion.mvp.ui.outsidetask.adapter.StatusAdapter;
import com.sdtv.mediunfusion.mvp.ui.outsidetask.constant.TaskStatusConstant;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 外采任务列表页
 * <p>
 * 头部状态选择以及搜索控件
 */

public class FilterView extends LinearLayout implements View.OnClickListener {

    private static final String LOG_TAG = "FilterView";

    @BindView(R.id.search_xiaLaImg)
    ImageView searchXiaLaImg;
    @BindView(R.id.filter_searchView)
    LinearLayout filterSearchView;
    @BindView(R.id.status_name)
    TextView statusName;
    @BindView(R.id.status_listView)
    ListView listView;
    @BindView(R.id.serch_button)
    TextView serchButton;
    @BindView(R.id.search_inputView)
    AutoCompleteTextView inputView;
    @BindView(R.id.search_parentPart)
    RelativeLayout searchParentPart;
    @BindView(R.id.status_parentView)
    LinearLayout statusParentView;
    @BindView(R.id.status_name_xiaLaImg)
    ImageView statusNameXiaLaImg;
    @BindView(R.id.filter_spaceView)
    View spaceView;

    private Context context;
    //状态adapter
    private StatusAdapter adapter;
    //获取数据的工具类
    private TaskStatusConstant dataModel;
    //状态数据
    private List<StatusBean> statusList;
    //键盘管理类
    private InputMethodManager inputMethodManager;
    //搜索关键词
    private String keyword = "";
    //分类内容 默认展示进行中的   (0：进行中；1：已完成；2：撤销中；3：已撤销)
    private String status = "0";
    // 选择按钮监听
    private ChooseTaskListener selectListener;

    public void setSearching(boolean searching) {
        isSearching = searching;
    }

    private boolean isSearching = false;

    public FilterView(Context context, ChooseTaskListener chooseTaskListener) {
        super(context);
        this.context = context;
        this.selectListener = chooseTaskListener;
        dataModel = new TaskStatusConstant();
        initUI();
    }

    private void initUI() {
        LayoutInflater.from(context).inflate(R.layout.filter_layout, this);
        ButterKnife.bind(this);
        //设置按钮的点击事件选中分类点击事件
        statusParentView.setOnClickListener(this);
        //搜索按钮
        filterSearchView.setOnClickListener(this);
        //搜索按钮
        serchButton.setOnClickListener(this);
        //设置状态数据
        setStatusData();
        inputMethodManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
    }

    //设置状态栏数据
    private void setStatusData() {
        statusList = dataModel.getStatusListData();
        //状态数据列表
        adapter = new StatusAdapter(context);
        adapter.setResultList(statusList);
        listView.setAdapter(adapter);
        //列表数据点击事件 --
        listView.setOnItemClickListener((parent, view, position, id) -> {
            StatusBean item = (StatusBean) parent.getItemAtPosition(position);
            status = item.getType();
            statusName.setText(item.getName());
            //将其余内容置为未选中状态
            statusList = dataModel.changeSelectItem(item);
            //更新列表选中状态
            adapter.setResultList(statusList);
            adapter.notifyDataSetChanged();
            //隐藏列表
            showStatusView(false);
            //进行搜索处理
            selectListener.getSelectStatusAndKey(status, keyword);
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.status_parentView:
                //隐藏搜索view
                searchParentPart.setVisibility(View.GONE);
                //切换箭头状态
                boolean isShow = (View.VISIBLE == listView.getVisibility());
                showStatusView(!isShow);
                break;

            case R.id.filter_searchView:
                //隐藏状态view
                listView.setVisibility(View.GONE);
                showSearchView(!(View.VISIBLE == searchParentPart.getVisibility()));
                break;
            case R.id.serch_button:
                CharSequence key = inputView.getText();
                if (null != key && key.toString().trim().length() > 0) {
                    //开始进行搜索
                    keyword = key.toString().trim();
                    //清空搜索框 并隐藏
                    selectListener.getSelectStatusAndKey(status, keyword);
                } else {
                    ToastUtils.showShort("请输入搜索内容");
                    //不输入关键字 搜索全部内容
                    selectListener.getSelectStatusAndKey(status, keyword);
                }
                isSearching = true;
                showSearchView(false);
                break;
        }
    }

    /**
     * 显示 或者隐藏状态列表数据
     *
     * @param isShow true: 显示列表   false:隐藏状态列表
     */
    private void showStatusView(boolean isShow) {
        if (isShow) {
            statusNameXiaLaImg.setImageResource(R.mipmap.ic_quanzi_shouqi);
            listView.setVisibility(View.VISIBLE);
            spaceView.setVisibility(View.VISIBLE);
        } else {
            statusNameXiaLaImg.setImageResource(R.mipmap.ic_quanzi_xiala);
            listView.setVisibility(View.GONE);
            spaceView.setVisibility(View.GONE);
        }
    }

    /**
     * 显示或者隐藏搜索框
     *
     * @param isShow
     */
    private void showSearchView(boolean isShow) {
        if (isShow) {
            searchXiaLaImg.setImageResource(R.mipmap.ic_quanzi_shouqi);
            searchParentPart.setVisibility(View.VISIBLE);
            spaceView.setVisibility(View.VISIBLE);
        } else {
            searchXiaLaImg.setImageResource(R.mipmap.ic_quanzi_xiala);
            searchParentPart.setVisibility(View.GONE);
            spaceView.setVisibility(View.GONE);
            if (null != inputMethodManager) {
                inputMethodManager.hideSoftInputFromWindow(inputView.getWindowToken(), 0);
            }
//            //清空搜索框内容
            inputView.setText("");
//            //将关键词置为空
            keyword = "";
        }
    }

    public boolean isSearching() {
        return isSearching;
    }

    public interface ChooseTaskListener {

        void getSelectStatusAndKey(String status, String key);
    }
}
